<template>
  <div class="container">
    <el-card>
      <el-table v-loading="loading" :data="List" border stripe>
        <el-table-column
          align="center"
          label="名称"
          prop="name"
        ></el-table-column>
        <el-table-column
          align="center"
          label="HOST"
          prop="host"
        ></el-table-column>
        <el-table-column align="center" label="操作" width="200px">
          <template slot-scope="{ row }">
            <el-button type="primary" size="mini" plain @click="showUpdate(row)"
              >编辑</el-button
            >
            <el-button type="danger" size="mini" plain @click="showDelete(row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </el-card>
    <el-dialog
      title="编辑"
      :visible.sync="update_vis"
      width="70%"
      @close="row = {}"
    >
      <el-form
        :model="row"
        ref="form"
        label-width="160px"
        :inline="false"
        size="normal"
      >
        <el-form-item label="名称">
          <el-input size="small" v-model="row.name"></el-input>
        </el-form-item>
        <el-form-item label="HOST">
          <el-input size="small" v-model="row.host"></el-input>
        </el-form-item>
        <el-form-item label="搜索地址">
          <el-input size="small" v-model="row.search_url"></el-input>
        </el-form-item>
        <el-form-item label="方法">
          <el-input size="small" v-model="row.method"></el-input>
        </el-form-item>
        <el-form-item label="Header">
          <el-input size="small" v-model="row.header"></el-input>
        </el-form-item>
        <el-form-item label="KEY">
          <el-input size="small" v-model="row.search_key"></el-input>
        </el-form-item>
        <el-form-item label="搜索表单">
          <el-input
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
            v-model="row.search_form"
          ></el-input>
        </el-form-item>
        <el-form-item label="列表内容正则">
          <el-input
            v-model="row.list_content_zz"
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="列表正则">
          <el-input
            v-model="row.item_zz"
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="简介正则">
          <el-input size="small" v-model="row.description_zz"></el-input>
        </el-form-item>
        <el-form-item label="图片正则">
          <el-input size="small" v-model="row.image_zz"></el-input>
        </el-form-item>
        <el-form-item label="类型正则">
          <el-input size="small" v-model="row.type_zz"></el-input>
        </el-form-item>
        <el-form-item label="完整目录">
          <el-input
            v-model="row.all_mulu_zz"
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="章节列表内容正则">
          <el-input
            v-model="row.list_pattern"
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="章节正则">
          <el-input
            v-model="row.chapter_pattern"
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="章节内容正则">
          <el-input
            v-model="row.content_pattern"
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="替换">
          <el-input
            v-model="row.replaceList"
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
        <el-form-item label="章节下一页">
          <el-input
            v-model="row.content_next_zz"
            size="small"
            :autosize="{ minRows: 4 }"
            type="textarea"
          ></el-input>
        </el-form-item>
      </el-form>

      <span slot="footer">
        <el-button size="small" @click="update_vis = false">取消</el-button>
        <el-button size="small" type="primary" @click="update">保存</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import Pagination from "@/components/Pagination";
import {
  getWebsite,
  updateWebsite,
  addWebsite,
  delWebsite
} from "@/api/website";
export default {
  components: { Pagination },
  data() {
    return {
      List: [],
      total: 0,
      update_vis: false,
      row: {},
      loading: false,
      listQuery: {
        page: 1,
        limit: 10
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    update() {
      const loading = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      let data = Object.assign({}, this.row);
      for (let key in data) {
        if (!data[key]) {
          delete data[key];
        }
      }
      updateWebsite(data)
        .then(res => {
          this.update_vis = false;
          this.$notify({
            title: "成功",
            message: "保存成功",
            type: "success"
          });
        })
        .finally(_ => {
          loading.close();
        });
    },
    showUpdate(row) {
      this.row = row;

      this.row.header = JSON.stringify(this.row.header);
      this.row.search_form = JSON.stringify(this.row.search_form);
      this.row.replaceList = JSON.stringify(this.row.replaceList);
      this.update_vis = true;
    },
    showDelete(row) {
      this.$confirm("确定删除?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        const loading = this.$loading({
          lock: true,
          text: "Loading",
          spinner: "el-icon-loading",
          background: "rgba(0, 0, 0, 0.7)"
        });
        delWebsite(row.id)
          .then(res => {
            this.$notify({
              title: "成功",
              message: "删除成功",
              type: "success"
            });
            this.getList();
          })
          .finally(_ => {
            loading.close();
          });
      });
    },
    getList() {
      this.loading = true;
      getWebsite(this.listQuery).then(res => {
        this.List = res.results;
        this.total = res.count;
        this.loading = false;
      });
    }
  }
};
</script>

<style scoped>
.box {
  display: flex;
}
.box .el-collapse {
  flex: 1;
}
.container >>> .el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  min-height: calc(100% - 30px);
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
}
.container >>> .el-dialog__body {
  flex: 1;
  overflow: auto;
}
.action {
  height: 48px;
  display: flex;
  align-items: center;
}
.action .add {
  font-size: 20px;
  padding: 5px;
  color: #409eff;
  cursor: pointer;
}
.action .delete {
  font-size: 20px;
  padding: 5px;
  color: #f56c6c;
  cursor: pointer;
}
</style>
